package org.example.jedis.pipeline;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;

import java.util.Set;

/**
 * @author kylin
 * @date 2020/4/18
 * @desc pipeline 批量读操作
 * <p>
 * @see PipelineSet 数据录入
 */
public class PipelineGet {
    public static void main(String[] args) {
        new Thread(() -> {
            Jedis jedis = new Jedis("192.168.165.7", 6379);

            Set<String> keys = jedis.keys("batch*");
            Pipeline pipelined = jedis.pipelined();
            long t1 = System.currentTimeMillis();
            for (String key : keys) {
                pipelined.get(key);
            }
//            List<Object> result = pipelined.syncAndReturnAll();
//            for (Object src : result) {
//                System.out.println(src);
//            }
            System.out.println("Pipeline " + pipelined.syncAndReturnAll().size() + "数据  get耗时：" + (System.currentTimeMillis() - t1) + "ms");
            // 耗时：2178ms 垃圾虚拟机测试
        }).start();
        new Thread(() -> {
            Jedis jedis = new Jedis("192.168.165.7", 6379);
            Set<String> keys = jedis.keys("batch*");
            Pipeline pipelined = jedis.pipelined();
            long t1 = System.currentTimeMillis();
            for (String key : keys) {
                pipelined.get(key);
            }
//            List<Object> result  = pipelined.syncAndReturnAll();
//            for (Object src : result) {
//                System.out.println(src);
//            }
            System.out.println("Pipeline " + pipelined.syncAndReturnAll().size() + "数据  get耗时：" + (System.currentTimeMillis() - t1) + "ms");
            // 耗时：2207ms 垃圾虚拟机测试
        }).start();
    }
}
